//
//  OptionMoreViewController.swift
//  SmartPenSwift
//
//  Created by tql on 2021/2/3.
//

import UIKit

protocol OptionMoreDelegate : NSObjectProtocol {
    func optionToGetRecognize()
    func optionToGetPenInfo()
    func optionToOTA()
    func optionToAbout()
    func optionToChooseTXT()
    
     
}
protocol OffLineDelegate {
    func ODToGetAllOfflineData()
    func ODToStopGetAllOfflineData()
    func ODToClearAllOfflineData()
    func ODToGetAllOfflineDataCount()
}


class OptionMoreViewController: UIViewController {

    weak open var delegateOM : OptionMoreDelegate?
     open var delegateOD : OffLineDelegate?
    
    var bShowOffLine : Bool = false
    lazy var tableView : UITableView = {
        let table = UITableView.init(frame: self.view.bounds, style: .plain)
        table.delegate = self
        table.dataSource = self
        table.register(MoreOptionTableViewCell.classForCoder(), forCellReuseIdentifier: "MoreOptionTableViewCell")
        table.layer.cornerRadius = 10
        table.layer.shadowColor = UIColor.black.cgColor
        table.layer.shadowOpacity = 0.5
        table.bounces = false
        table.isScrollEnabled = false
        return table
    }()
    let moreOptionArray : Array<String> = ["笔迹识别","离线数据","笔参数","OTA升级","选择TXT","关于"]
    let offLineArray : Array<String> = ["获取数据","停止获取","清除数据","离线数据量"]
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .clear
        self.view.addSubview(self.tableView)
        self.tableView.snp.makeConstraints { (make) in
            make.edges.equalTo(self.view)
        }
        
        
    }
    
    @objc func returntoMoreOptionView(){
        bShowOffLine = false
        tableView.reloadData()
    }
    
     
}


extension OptionMoreViewController : UITableViewDelegate{
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        debugPrint(#function)
        if bShowOffLine == false {
            
            if indexPath.row == 1 {
                bShowOffLine = true
                tableView.reloadData()
                return
            }
            
            if indexPath.row == 0 {
                if self.delegateOM != nil   {
                    delegateOM?.optionToGetRecognize()
                }
            }else if indexPath.row == 2 {
                if self.delegateOM != nil   {
                    delegateOM?.optionToGetPenInfo()
                }
            }else if indexPath.row == 3 {
                if self.delegateOM != nil   {
                    delegateOM?.optionToOTA()
                }
            }else if indexPath.row == 4 {
                if self.delegateOM != nil   {
                    delegateOM?.optionToChooseTXT()
                }
            }else if indexPath.row == 5 {
                if self.delegateOM != nil   {
                    delegateOM?.optionToAbout()
                }
            }else{
                debugPrint("others ")
            }
            
        }else{
            if indexPath.row == 0 {
                if self.delegateOD != nil {
                    self.delegateOD!.ODToGetAllOfflineData()
                }
            }else if indexPath.row == 1 {
                if self.delegateOD != nil {
                    self.delegateOD!.ODToStopGetAllOfflineData()
                }
            }else if indexPath.row == 2 {
                if self.delegateOD != nil {
                    self.delegateOD!.ODToClearAllOfflineData()
                }
            }else if indexPath.row == 3 {
                if self.delegateOD != nil {
                    self.delegateOD!.ODToGetAllOfflineDataCount()
                }
            }
        }
    }
}


extension OptionMoreViewController : UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if bShowOffLine == true {
            return offLineArray.count
        }
        return moreOptionArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell : MoreOptionTableViewCell = MoreOptionTableViewCell.init(style: .default, reuseIdentifier: "MoreOptionTableViewCell")
        
        if bShowOffLine == true {
            cell.name  = offLineArray[indexPath.row]
            cell.textLabel?.text = offLineArray[indexPath.row]
            cell.textLabel?.font = UIFont.systemFont(ofSize: 10)
            cell.imageView?.image = UIImage.init(named: offLineArray[indexPath.row] + "_N")
            cell.accessoryType = .none
        }else{
            cell.name  = moreOptionArray[indexPath.row]
            cell.textLabel?.text = moreOptionArray[indexPath.row]
            cell.textLabel?.font = UIFont.systemFont(ofSize: 10)
            cell.imageView?.image = UIImage.init(named: moreOptionArray[indexPath.row] + "_N")
            if indexPath.row == 1 {
                cell.accessoryType = .disclosureIndicator
            }else{
                cell.accessoryType = .none
            }
        }
        
        return cell
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 40
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 40
    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        if bShowOffLine == true {
            let view : UIView = UIView.init(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: tableView.sectionHeaderHeight))
            let rebackbtn : UIButton = UIButton.init(frame: CGRect(x: 0, y: 0, width: view.frame.size.width - 50, height: view.frame.size.height))
            rebackbtn.setTitle("   选项", for: .normal)
            rebackbtn.setTitleColor(UIColor.init(hex: "132238"), for: .normal)
            rebackbtn.setImage(UIImage.init(named: "icon-返回"), for: .normal)
            rebackbtn.contentHorizontalAlignment = .left
            rebackbtn.addTarget(self, action: #selector(returntoMoreOptionView), for: .touchUpInside)
            view.addSubview(rebackbtn)
            rebackbtn.snp.makeConstraints { (make) in
                make.bottom.top.equalTo(view)
                make.left.equalTo(view).offset(20)
                make.right.equalTo(view).offset(-30)
            }
            
            return view
        }else{
            let view : UIView = UIView.init(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: tableView.sectionHeaderHeight))
            let label : UILabel = UILabel.init(frame: view.frame)
            view.addSubview(label)
            label.snp.makeConstraints { (make) in
                make.edges.equalTo(view)
            }
            
            label.textAlignment = .center
            label.font = UIFont.systemFont(ofSize: 10)
            label.text = "选项"
            label.adjustsFontSizeToFitWidth = true
            return view
        }
    }
}





